import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/login',
    component: () => import('views/Login.vue'),
    meta: { isPublic: true }
  },
  {
    path: '/home',
    component: () => import('views/Home.vue'),
    redirect: '/categories/list',
    children: [
      // 分类
      {
        path: '/categories/create',
        component: () => import('views/Category/Category.vue')
      },
      {
        path: '/categories/list',
        component: () => import('views/Category/List.vue')
      },
      {
        path: '/categories/edit/:id',
        component: () => import('views/Category/Category.vue'),
        props: true
      },
      // 装备
      {
        path: '/items/create',
        component: () => import('views/Item/Item.vue'),
      },
      {
        path: '/items/list',
        component: () => import('views/Item/List.vue'),
      },
      {
        path: '/items/edit/:id',
        component: () => import('views/Item/Item.vue'),
        props: true
      },
      // 英雄
      {
        path: '/heros/create',
        component: () => import('views/Hero/Hero.vue'),
      },
      {
        path: '/heros/list',
        component: () => import('views/Hero/List.vue'),
      },
      {
        path: '/heros/edit/:id',
        component: () => import('views/Hero/Hero.vue'),
        props: true
      },
      // 文章
      {
        path: '/articles/create',
        component: () => import('views/Article/ArticleEdit.vue'),
      },
      {
        path: '/articles/list',
        component: () => import('views/Article/ArticleList.vue'),
      },
      {
        path: '/articles/edit/:id',
        component: () => import('views/Article/ArticleEdit.vue'),
        props: true
      },
      // 广告位
      {
        path: '/ads/create',
        component: () => import('views/Ad/AdEdit.vue'),
      },
      {
        path: '/ads/list', 
        component: () => import('views/Ad/AdList.vue'),
      },
      {
        path: '/ads/edit/:id',
        component: () => import('views/Ad/AdEdit.vue'),
        props: true
      },
      // 管理员
      {
        path: '/admin-user/create',
        component: () => import('views/Admin/AdminUserEdit.vue'),
      },
      {
        path: '/admin-user/list', 
        component: () => import('views/Admin/AdminUserList.vue'),
      },
      {
        path: '/admin-user/edit/:id',
        component: () => import('views/Admin/AdminUserEdit.vue'),
        props: true
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
  if (!to.meta.isPublic && !localStorage.token) {
    return next('/login');
  }
  next();
})

export default router
